var dateFormat = require('dateformat');

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}


/**
 *
 * @param strTime 2020-05-08T12:05:42+08:00
 * @returns {string} 2020-05-08 12:05:42
 */
export const utilFormatStrTime = strTime => {
    if (!strTime) return "";
    // console.log(strTime)
    // console.log(new Date(strTime))
    // let d = new Date(strTime.substring(0, 4), strTime.substring(4, 6), strTime.substring(6, 8), strTime.substring(8, 10), strTime.substring(10, 12), strTime.substring(12, 14))
    // console.log(d)
    // return dateFormat(d, 'yyyy-MM-dd hh:mm:ss')
    return strTime.substring(0, 10) + " " + strTime.substring(11, 19)
}


/**
 *
 * @param strTime 2020-05-08T12:05:42+08:00
 * @returns {int} 秒
 */
export const utilDiffStrTimeAndNow = strTime => {
    if (!strTime) return 9999999999;
    // var dateBegin = new Date(d1.replace(/-/g, "/"));//将-转化为/，使用new Date
    var dateBegin = new Date(strTime);//将-转化为/，使用new Date
    var dateEnd = new Date();//获取当前时间
    var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
    // console.log(dateBegin, dateEnd, dateDiff)
    // if (!strTime) return;
    // console.log(strTime)
    // console.log(new Date(strTime))
    return dateDiff / 1000
}

/***
 *
 * @param strMoney 1222
 * @returns {string} ￥12.22元
 */
export const utilFormatMoneyFen2Yuan = strMoney => {
    if (!strMoney) return "￥0元";
    // console.log(strMoney)
    // console.log(new Date(strTime))
    // let d = new Date(strTime.substring(0, 4), strTime.substring(4, 6), strTime.substring(6, 8), strTime.substring(8, 10), strTime.substring(10, 12), strTime.substring(12, 14))
    // console.log(d)
    // return dateFormat(d, 'yyyy-MM-dd hh:mm:ss')
    return "￥" + strMoney.substring(0, strMoney.length - 2) + "." + strMoney.substring(strMoney.length - 2) + "元"
}


export const str2Number = str => {
    if (!str || str == "") return 0;
    // console.log(str)
    return Number(str)
}
